package com.benshou.bcss.common.asserts;

import com.benshou.bcss.common.exception.BizException;
import com.benshou.bcss.common.j17common.Requires;
import com.benshou.bcss.common.j17common.annotation.NotBlank;
import com.benshou.bcss.common.j17common.annotation.NotEmpty;
import com.benshou.bcss.common.j17common.annotation.NotNull;
import lombok.NonNull;

import java.util.Collection;
import java.util.Map;
import java.util.function.Predicate;
import java.util.function.Supplier;

/**
 * 业务断言类
 *
 * @author <a href="mailto:liuzhenbang@benshou.com">Liu Zhenbang</a>
 */
public class BizAssert {

    public static boolean isTrue(Boolean condition, String errMsg) throws BizException {
        return Requires.isTrue(condition, toBizExceptionSupplier(errMsg));
    }

    public static boolean isTrue(Boolean condition) throws BizException {
        return isTrue(condition, null);
    }

    public static <T> T pass(
            T obj,
            @NonNull Predicate<? super T> predicate,
            String errMsg) throws BizException {
        return Requires.pass(obj, predicate, toBizExceptionSupplier(errMsg));
    }

    public static <T> T pass(T obj, @NonNull Predicate<? super T> predicate) throws BizException {
        return pass(obj, predicate, null);
    }

    @NotNull
    public static <T> T notNull(T obj, String errMsg) throws BizException {
        return Requires.notnull(obj, toBizExceptionSupplier(errMsg));
    }

    @NotNull
    public static <T> T notNull(T obj) throws BizException {
        return notNull(obj, null);
    }

    @NotNull
    @NotEmpty
    public static <T> T[] notEmpty(T[] array, String errMsg) throws BizException {
        return Requires.notEmpty(array, toBizExceptionSupplier(errMsg));
    }

    @NotNull
    @NotEmpty
    public static <T> T[] notEmpty(T[] array) throws BizException {
        return notEmpty(array, null);
    }

    @NotNull
    @NotEmpty
    public static <C extends Collection<?>> C notEmpty(C collection, String errMsg) throws BizException {
        return Requires.notEmpty(collection, toBizExceptionSupplier(errMsg));
    }

    @NotNull
    @NotEmpty
    public static <C extends Collection<?>> C notEmpty(C collection) throws BizException {
        return notEmpty(collection, null);
    }

    @NotNull
    @NotEmpty
    public static <M extends Map<?, ?>> M notEmpty(M map, String errMsg) throws BizException {
        return Requires.notEmpty(map, toBizExceptionSupplier(errMsg));
    }

    @NotNull
    @NotEmpty
    public static <M extends Map<?, ?>> M notEmpty(M map) throws BizException {
        return notEmpty(map, null);
    }

    @NotNull
    @NotBlank
    public static String notBlank(String string, String errMsg) throws BizException {
        return Requires.notBlank(string, toBizExceptionSupplier(errMsg));
    }

    @NotNull
    @NotBlank
    public static String notBlank(String string) throws BizException {
        return notBlank(string, null);
    }

    private static Supplier<BizException> toBizExceptionSupplier(String errMsg) {
        return () -> new BizException(errMsg);
    }
}
